package com.ndhutu.ee.util.push.jg;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.ndhutu.ee.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;


/**
 * 极光推送工具类
 * @Author Shane
 * @Date 2019/10/2210:15
 * @param
 * @return
 **/
public class PushUtil {
    static Logger log = LoggerFactory.getLogger(PushUtil.class);

    /**
     * 推送消息
     * @Author Shane
     * @Date 2019/10/2211:30
     * @param tag   标签  user-所有用户 host-主播用户
     * @param alisa 别名
     * @param title 消息标题
     * @param content 消息内容
     * @param data  传递的json格式数据
     * @return
     **/
    public static void push(String tag, String alisa, String title, String content, Map<String, String> data, PushConfig config){
        JPushClient jpushClient = new JPushClient(config.getMasterSecret(), config.getAppKey(), null, ClientConfig.getInstance());

        PushPayload payload = buildPushObject(tag, alisa, title, content, data);

        try {
            PushResult result = jpushClient.sendPush(payload);
            log.info("Got result - " + result);

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            log.error("Connection error, should retry later", e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            log.error("Should review the error, and fix the request", e);
            log.error("HTTP Status: " + e.getStatus());
            log.error("Error Code: " + e.getErrorCode());
            log.error("Error Message: " + e.getErrorMessage());
        }
    }

    /**
     * 构建推送对象
     * @Author Shane
     * @Date 2019/10/2211:30
     * @param tag   标签
     * @param alisa 别名
     * @param title 消息标题
     * @param content 消息内容
     * @param data  传递的json格式数据
     * @return
     **/
    private static PushPayload buildPushObject(String tag, String alisa, String title, String content, Map<String, String> data){
        Audience.Builder audienceBuilder = Audience.newBuilder();
        if(StringUtil.isEmpty(tag) && StringUtil.isEmpty(alisa)){
            audienceBuilder.setAll(true);
        }else{
            if(!StringUtil.isEmpty(tag)){
                audienceBuilder.addAudienceTarget(AudienceTarget.tag(tag));
            }
            if(!StringUtil.isEmpty(alisa)){
                audienceBuilder.addAudienceTarget(AudienceTarget.alias(alisa));
            }
        }

        Notification.Builder notificationBuilder = Notification.newBuilder();
        notificationBuilder.addPlatformNotification(
                AndroidNotification.newBuilder()
                        .setAlert(content)
                        .setTitle(title).build())
        .addPlatformNotification(
                IosNotification.newBuilder()
                        .setAlert(content).build()
        );

        Message.Builder messageBuilder = Message.newBuilder();
        messageBuilder.setMsgContent(content)
                .setTitle(title)
                .addExtras(data);

        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(audienceBuilder.build())
                .setNotification(notificationBuilder.build())
                .setMessage(messageBuilder.build())
                .build();
    }

    public static void main(String[] args) throws Exception{
        PushConfig config = new PushConfig() {
            public String getAppKey() {
                return "48ab5a048e8d29f03b4c0e81";
            }

            public String getMasterSecret() {
                return "0d68e6055bc24527043df0b2";
            }
        };

        Map<String, String> data = new HashMap<String, String>();
        data.put("title", "123456");

        push(null, "2351855", "测试标题", "测试消息", data, config);
    }
}
